/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.netbrain.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import mx.netbrain.dao.OrganizationDao;
import mx.netbrain.dao.RepositoriesDao;
import mx.netbrain.dao.impl.OrganizationDaoImpl;
import mx.netbrain.dao.impl.RepositoriesDaoImpl;
import mx.netbrain.model.Organization;
import mx.netbrain.model.Repositories;
import mx.netbrain.model.Status;

/**
 *
 * @author gamma
 */
@ManagedBean(name = "repositoriesBean")
@SessionScoped
public class RepositoriesBean implements Serializable {

    private Repositories repositories = new Repositories();
    private Organization organization;
    private Status status = new Status();
    private List<Organization> listOrganization = new ArrayList<Organization>();
    private List<Repositories> listRepositories;
    private String nameRepo;
    private String descRepo;
    private String assignedOrg;

    public RepositoriesBean() {
        OrganizationDao organizationDao = new OrganizationDaoImpl();
        listOrganization = organizationDao.listAll();
    }
    
    public void valorVariable(){
        System.out.println("====> " + organization.getName());
    }
    
    public void saveRepo() {
        RepositoriesDao repositoriesDao = new RepositoriesDaoImpl();
        repositories.setName(nameRepo);
        repositories.setDescription(descRepo);
        status.setId(1);
        repositories.setStatus(status);
        repositories.setRegistro(new Date());
        System.out.println(organization + "<=== organization");
        repositories.setOrganization(organization);
        repositoriesDao.save(repositories);
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage("¡El registro se guardó correctamente!", "Guardado"));
        repositories = new Repositories();
        nameRepo = new String();
        descRepo = new String();
        organization = new Organization();
    }

    public List<Organization> completeNameOrg(String query) {

        List<Organization> suggestions = new ArrayList<Organization>();
        
        for (Organization o : listOrganization) {
            if (o.getName().startsWith(query)) {
                suggestions.add(o);
            }
        }
        return suggestions;
    }

    public Organization getOrganization() {
        return organization;
    }

    public void setOrganization(Organization organization) {
        this.organization = organization;
    }

    public Repositories getRepositories() {
        return repositories;
    }

    public void setRepositories(Repositories repositories) {
        this.repositories = repositories;
    }

    public List<Organization> getListOrganization() {
        return listOrganization;
    }

    public void setListOrganization(List<Organization> listOrganization) {
        this.listOrganization = listOrganization;
    }

    public List<Repositories> getListRepositories() {
        RepositoriesDao repositoriesDao = new RepositoriesDaoImpl();
        listRepositories = repositoriesDao.listAll();
        return listRepositories;
    }

    public void setListRepositories(List<Repositories> listRepositories) {
        this.listRepositories = listRepositories;
    }

    public String getNameRepo() {
        return nameRepo;
    }

    public void setNameRepo(String nameRepo) {
        this.nameRepo = nameRepo;
    }

    public String getDescRepo() {
        return descRepo;
    }

    public void setDescRepo(String descRepo) {
        this.descRepo = descRepo;
    }

    public String getAssignedOrg() {
        return assignedOrg;
    }

    public void setAssignedOrg(String assignedOrg) {
        this.assignedOrg = assignedOrg;
    }
    
}
